<?php
/**
 * 处理用户牢骚的发布，回复更改等。
 *
 *
 * @author 纹身特湿 <judasnow@gmail.com>
 */
class user_complaint_process{

	private $db_connect;
	private $db_obj_user_complaint;	
	 
	private $email;

	/**
	 * 传入数据库连接，以及当前用户的登录凭证。
	 *
	 * @param object $db_connect
         * @param string $email	 
	 */
	function __construct( $db_connect ){

		$this->db_obj_user_complaint = new db_obj_user_complaint( $db_connect );
		$this->db_obj_user = new db_obj_user( $db_connect );

		$this->email = session::get('email');
	}

	function post_new( $request ){

		//发牢骚的时间
		$time = date( 'Y-m-d H.i.s' );
		//牢骚的内容
		$content = $request['content'];
		//发牢骚的方式
		$method = 'web';

		$this->db_obj_user_complaint->set( 'email' , $this->email );
		$this->db_obj_user_complaint->set( 'time', $time );
		$this->db_obj_user_complaint->set( 'content' , $content );
		$this->db_obj_user_complaint->set( 'method' , $method );

		$this->db_obj_user_complaint->set( 'hash' , 
			 $this->db_obj_user_complaint->get_hash() ); 

		return $this->db_obj_user_complaint->save();
	}

	function replay( $request ){
	}
	//更新功能可有可无
	function update(){
	}
	//根据where条件删除牢骚
	function delete( $where ){
	}

	/**
	 * 得到指定区间的n条信息
	 * 默认以时间升序排序
	 *
	 */
	function get_complaint( $max_page , $page = 1 , $page_size = 3 , $sort = array( 'time' => -1 )){

		$where = array( 'email' => $this->email );

		$cursor = $this->db_obj_user_complaint->collection->find( $where );
		//需要跳过的记录数
		$skip = ($page-1) * $page_size;
		$skip = $skip<0 ? 0 : $skip;

		$max_page = ceil( $cursor->count() / $page_size );

		if( $page > $max_page || $page <= 0 ){
		
			echo '到头啦！';
			return false;
		}

	 	return $cursor->sort( $sort )->skip( $skip )->limit( $page_size );
	}
}
